package org.monkey.common.constant.entity;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.monkey.common.constant.util.DataUtil;

@Data
public class MonkeyPage<T> {
  private long current;
  private long pages;
  private List<T> records = new ArrayList<>();
  private long size;
  private long total;

  public static <T> MonkeyPage<T> listToPage(int pageNumber, int pageSize, List<T> ts) {
    MonkeyPage<T> shiningPage = new MonkeyPage<>();
    long total = ts.size();
    shiningPage.setCurrent(pageNumber);
    shiningPage.setSize(pageSize);
    List<T> results = new ArrayList<>();
    if (DataUtil.isEmpty(ts)) {
      shiningPage.setPages(1);
      shiningPage.setRecords(results);
      shiningPage.setTotal(0);
    } else {
      shiningPage.setTotal(ts.size());
      if (total < pageSize) {
        shiningPage.setPages(1);
      } else {
        if (total % pageSize == 0) {
          shiningPage.setPages(total / pageSize);
        } else {
          shiningPage.setPages(total / pageSize + 1);
        }
      }
      int pageStart = pageNumber == 1 ? 0 : (pageNumber - 1) * pageSize;
      if (pageStart < ts.size()) {
        for (int i = pageStart; i < pageStart + pageSize && i < ts.size(); i++) {
          results.add(ts.get(i));
        }
      }
      shiningPage.setRecords(results);
    }
    return shiningPage;
  }
}
